package com.goddess.yeecolor.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.goddess.yeecolor.Modle.FirstBean;
import com.goddess.yeecolor.Modle.SecondBean;
import com.goddess.yeecolor.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/7/15.
 */
public class ExpandableListViewAdapterOne extends BaseExpandableListAdapter {
    private Context mContext;
    private ArrayList<FirstBean> mData;
    ViewHolder holder = null;
    public ExpandableListViewAdapterOne(Context context, ArrayList<FirstBean> data){
        this.mContext = context;
        this.mData = data;
    }
    @Override
    public int getGroupCount() {


        return mData != null?mData.size():0;
    }

    @Override
    public int getChildrenCount(int parentPosition) {
        return mData.get(parentPosition).getSecondBeen().size();
    }

    @Override
    public Object getGroup(int parentPosition) {
        return mData.get(parentPosition);
    }

    @Override
    public SecondBean getChild(int parentPosition, int childPosition) {
        return mData.get(parentPosition).getSecondBeen().get(childPosition);
    }

    @Override
    public long getGroupId(int parentPosition) {
        return parentPosition;
    }

    @Override
    public long getChildId(int parentPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public void notifyDataSetChanged() {
    }

    /**
     *  第一级菜单适配器布局
     * @param parentPosition
     * @param isExpanded
     *
     * @param convertView
     * @param viewGroup
     * @return
     */
    @Override
    public View getGroupView(int parentPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.expandable_listview_one_item, null);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setText(mData.get(parentPosition).getTitle());
        convertView.setClickable(true);

        return convertView;
    }
    class ViewHolder{
        private TextView title;
    }
    public ExpandableListView getExpandableListView() {
        ExpandableListView mExpandableListView = new ExpandableListView(
                mContext);
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) mContext.getResources().getDimension(R.dimen.parent_list_height));
        mExpandableListView.setLayoutParams(lp);
        mExpandableListView.setDividerHeight(1);// 取消group项的分割线
       // mExpandableListView.setChildDivider(R.color.pru);// 取消child项的分割线

        mExpandableListView.setGroupIndicator(null);// 取消展开折叠的指示图标
        return mExpandableListView;
    }
    /**
     * 第二级菜单式配
     * @param parentPosition
     * @param childPosition
     * @param isExpanded
     * @param view
     * @param viewGroup
     * @return
     */
    @Override
    public View getChildView(final int parentPosition, final int childPosition, boolean isExpanded, View view, ViewGroup viewGroup) {
        final ExpandableListView childListView = getExpandableListView();
        //获取子菜单的数据
        final ArrayList<SecondBean> childData = new ArrayList<SecondBean>();
        final SecondBean bean = getChild(parentPosition,childPosition);
        childData.add(bean);
        ExpandableListViewAdapterTo adapter = new ExpandableListViewAdapterTo(mContext,childData,parentPosition);
        childListView.setAdapter(adapter);
        /**
         *子ExpandableListView展开时，因为group只有一项，所以子ExpandableListView的总高度=
         * （子ExpandableListView的child数量 + 1 ）* 每一项的高度
         * */
        childListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                Log.e("xxx",groupPosition+"onGroupExpand>>");
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        (mData.size() + 1)* (int) mContext
                        .getResources().getDimension(R.dimen.parent_list_height));
                childListView.setLayoutParams(lp);
            }
        });
        /**
         * 在这里对二级菜单的点击事件进行操作
         */
        childListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int Position, long id) {
          //      Log.e("Xxx","恭喜你,点击了"+parentPosition+"childpos>>>"+childPosition+"webview"+childData.get(childPosition).getWebview());
                onItemClickListener.OnItemClick(v,parentPosition,childPosition);

                return true;
            }
        });

        return childListView;
    }
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    public interface OnItemClickListener{
        /**
         *
         * @param view   点击的试图
         * @param parentPosition //父级被点击单行id
         * @param childPosition//子级被点击单行id
         */
        void OnItemClick(View view,int parentPosition,int childPosition);
    }
    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

}
